<style>
    #video-update {
        padding: 20px 25px 25px 0;
    }
</style>
<div class="layui-fluid" id="video-update">
    <form class="layui-form" action="" lay-filter="video-update-form">
        <div class="layui-form-item febs-hide">
            <label class="layui-form-label febs-form-item-require">视频id：</label>
            <div class="layui-input-block">
                <input type="text" name="id" data-th-value="${video.id}" id="videoid">
            </div>
        </div>
        <div class="layui-form-item febs-hide">
            <label class="layui-form-label febs-form-item-require">状态：</label>
            <div class="layui-input-block">
                <input type="text" name="status" data-th-value="${video.status}">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label febs-form-item-require">名称：</label>
            <div class="layui-input-block">
                <input type="text" name="name" autocomplete="off" class="layui-input" id="videoname"
                       minlength="2" maxlength="50" lay-verify="range">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">简介：</label>
            <div class="layui-input-block">
                <textarea name="info" maxlength="100" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">视频：</label>
            <div class="layui-upload">
                <div class="layui-upload-list">
                    <video class="layui-upload-video" src="" id="video1" controls="controls" width="200px" height="150px">
                    </video>
                    <input type="hidden" id="myvideo" name="videoUrl">
                    <p id="videoText"></p >
                </div>
                <a id="testVideo" style="padding-left: 110px">上传视频</a>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">封面：</label>
            <div class="layui-upload">
                <div class="layui-upload-list">
                    <img class="layui-upload-img" id="img1" width="200px" height="150px">
                    <input type="hidden" id="myimg" name="imgUrl">
                    <p id="demoText"></p >
                </div>
                <a id="test1" style="padding-left: 110px">上传封面图片</a>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label febs-form-item-require">分类：</label>
            <div class="layui-input-block" id="video-update-type"></div>
        </div>
        <div class="layui-form-item febs-hide">
            <button class="layui-btn" lay-submit="" lay-filter="video-update-form-submit" id="submitVideo"></button>
            <button type="reset" class="layui-btn" id="reset"></button>
        </div>
    </form>
</div>

<script data-th-inline="javascript">
    layui.use(['febs', 'form', 'xmSelect', 'validate','upload'], function () {
        let $ = layui.$,
            febs = layui.febs,
            layer = layui.layer,
            form = layui.form,
            xmSelect = layui.xmSelect,
            upload = layui.upload,
            video = [[${video}]],
            typeId = [[${typeId}]],
            //courseId = [[${courseId}]],
            validate = layui.validate,
            //courseXmSelect,
            typeXmSelect;

        form.verify(validate);
        form.render();
        //检查视频名称标识符
        var videoNameFlag=true;

        initVideoValue();
        console.log(video);
        $('#video1').attr('src', video.videoUrl);
        $('#img1').attr('src', video.imgUrl);

        //视频上传
        var uploadVideo = upload.render({
            elem: '#testVideo'
            ,url: ctx+'upload' //改成您自己的上传接口
            ,method:'post'
            ,auto:false
            ,accept:'video'
            ,exts: 'mp4|avi'
            ,choose: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#video1').attr('src', result);
                    obj.upload(index, file);
                });
            }
            ,done: function(res){
                //上传成功
                if(res.code == 200){
                    layer.msg('上传视频成功');
                    $("#myvideo").val(res.url);
                }
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#videoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadVideo.upload();
                });
            }
        });

        //图片上传
        var uploadInst = upload.render({
            elem: '#test1'
            ,url: ctx+'upload' //改成您自己的上传接口
            ,method:'post'
            ,auto:false
            ,accept:'images'
            ,exts: 'jpg|png|gif|bmp|jpeg'
            ,choose: function(obj){
                //预读本地文件示例，不支持ie8
                var flag = true;
                obj.preview(function(index, file, result){
                    var img = new Image();
                    img.src = result;
                    img.onload = function () { //初始化夹在完成后获取上传图片宽高，判断限制上传图片的大小。
                        if(img.width <=750 && img.height <=560){
                            $('#img1').attr('src', result); //图片链接（base64）
                            obj.upload(index, file); //满足条件调用上传方法
                        }else{
                            flag = false;
                            layer.msg("您上传的小图大小必须小于750*560尺寸！");
                            return false;
                        }
                    }
                    return flag;
                });
            }
            ,done: function(res){
                //上传成功
                if(res.code == 200){
                    layer.msg('上传封面图片成功');
                    $("#myimg").val(res.url);
                }
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });

        typeXmSelect = xmSelect.render({
            el: '#video-update-type',
            model: {label: {type: 'text'}},
            radio:true,
            clickClose: true,
            tree: {
                show: true,
                strict: false,
                showLine: true,
                clickCheck: true,
                expandedKeys: true,
            },
            name: 'typeId',
            theme: {
                color: '#52c41a',
            },
            prop: {
                value: 'id'
            },
            height: '500px'
        });

        febs.get(ctx + 'cType/select/treexk', null, function (data) {
            typeXmSelect.update(data);
            typeXmSelect.setValue([typeId]);
        });

        function initVideoValue() {
            form.val("video-update-form", {
                "name": video.name,
                "videoUrl": video.videoUrl,
                "imgUrl": video.imgUrl,
                "info": video.info
            });
        }
        form.on('submit(video-update-form-submit)',function (data){
            if(!videoNameFlag){
                febs.alert.error("视频名称重复，请重新输入");
                return false;
            }
            if (!data.field.typeId) {
                febs.alert.warn('请选择课程分类！');
                return false;
            }
            febs.post(ctx + 'video/update', data.field, function () {
                layer.closeAll();
                febs.alert.success('更新课程成功');
                $('#businesses-video').find('#queryVideo').click();
            });
            return false;
        });
        //检查视频名称是否重复
        $(":text[name=name]").blur(function () {
            var videoname = $("#videoname").val();
            var videoid = $("#videoid").val();
            $.ajax({
                type:"get",
                url:'video/checkupdate',
                data:{"videoname":videoname,"videoid":videoid},
                success:function(res){
                    if(res){//返回结果为null，没有找到指定视频名称
                        videoNameFlag=true;
                    }else {//返回结果不为null，找到了指定视频名称
                        febs.alert.error("视频名称重复，请重新输入");
                        videoNameFlag=false;
                    }
                }
            });
        });
    });
</script>